
#include "ringBuff.h"



unsigned char IsRingBufferFull(stRingBuff *ringBuf)
{
    if(((ringBuf->in+1) % RING_BUFF_SIZE) == ringBuf->out)
    {
        return 1;
    }
    return 0;
}


unsigned char IsRingBufferEmpty(stRingBuff *ringBuf)
{
    if(ringBuf->in == ringBuf->out)   //写入位置和读出位置相等时为空
    {
        return 1;
    }
    return 0;
}





unsigned char WriteOneByteToRingBuffer(stRingBuff *ringBuf,char d)
{
    if(IsRingBufferFull(ringBuf))   //写之前先判断队列是否写满
    {
        return 0;
    }
    ringBuf->buffer[ringBuf->in] = d;
    ringBuf->in = (++ringBuf->in) % RING_BUFF_SIZE;    //防止越界
	  return 1;
}




unsigned char ReadOneByteFromRingBuffer(stRingBuff *ringBuf,char *d)
{
    if(IsRingBufferEmpty(ringBuf))    //读之前判断队列是否为空
    {
        return 0;
    }
    *d = ringBuf->buffer[ringBuf->out];
    ringBuf->out = (++ringBuf->out) % RING_BUFF_SIZE;    //防止越界
    return 1;
} 



void WriteRingBuffer(stRingBuff *ringBuf, char *writeBuf,unsigned int len)
{
    unsigned int i;

    for(i = 0; i < len; i++)
    {
        WriteOneByteToRingBuffer(ringBuf,writeBuf[i]);
    }
}


void ReadRingBuffer(stRingBuff *ringBuf,char *readBuf,unsigned int len)
{
    unsigned int i;
	
    for(i = 0; i < len; i++)
    {
        ReadOneByteFromRingBuffer(ringBuf,&readBuf[i]);
    }
}



unsigned int GetRingBufferLength(stRingBuff *ringbuf)
{
    return (ringbuf->in - ringbuf->out + RING_BUFF_SIZE) % RING_BUFF_SIZE;
}
